package com.webmarmun;


import java.awt.Image;
import java.awt.Toolkit;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Izmena extends javax.swing.JFrame {
     public String serverName = "localhost";
    public String mydatabase = "porotnici";
    public String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8";
    public String username = "root";
    public String password = "";
    public int BrojSati;
    float rsq = 0;
    float prev = 0;
    float pau = 0;

    public Izmena() {
        initComponents();
        java.net.URL url1 = ClassLoader.getSystemResource("com/webmarmun/camera.png");
        Toolkit kit = Toolkit.getDefaultToolkit();
        Image img = kit.createImage(url);
        this.setIconImage(img);
        try {

            java.sql.Connection connection = null;
            connection = DriverManager.getConnection(url, username, password);

            java.sql.PreparedStatement t = connection.prepareStatement("SELECT * from porotnik");
            ResultSet trs = null;
            trs = t.executeQuery();
            while (trs.next()) {
                jComboBox1.addItem(trs.getString("ime"));
            }
            }catch(SQLException ex){
                 Logger.getLogger(Izmena.class.getName()).log(Level.SEVERE, null, ex);
            }
        jComboBox1.setSelectedItem(TableFromDatabase.POROTNIK);
         try {

            java.sql.Connection connection = null;
            connection = DriverManager.getConnection(url, username, password);

            java.sql.PreparedStatement t = connection.prepareStatement("SELECT * from sudije");
            ResultSet trs = null;
            trs = t.executeQuery();
            while (trs.next()) {
                jComboBox2.addItem(trs.getString("Prezime_Ime_Sudije"));
            }
            }catch(SQLException ex){
                 Logger.getLogger(Izmena.class.getName()).log(Level.SEVERE, null, ex);
            }
        jComboBox2.setSelectedItem(TableFromDatabase.SUDIJA);
        su.setText(TableFromDatabase.SU);
        mesto.setText(TableFromDatabase.MESTO);
        jDateChooser1.setDateFormatString("dd-MM-yyyy");
        brojsati.setText(Integer.toString(TableFromDatabase.BROJSATI));
        radnisat.setText(Float.toString(TableFromDatabase.RADNISAT));
        prevoz.setText(Float.toString(TableFromDatabase.PREVOZ));
        pausalno.setText(Float.toString(TableFromDatabase.PAUSALNO));
        sudija.setText(TableFromDatabase.SUDIJA);
        ukupno.setText(Float.toString(TableFromDatabase.UKUPNO));
        napomena.setText(TableFromDatabase.NAPOMENA);

        String str_date = TableFromDatabase.DATUM;
        DateFormat formatter;
        Date date;
        formatter = new SimpleDateFormat("dd-MM-yyyy");
        try {
            date = (Date) formatter.parse(str_date);
            jDateChooser1.setDate(date);
        } catch (ParseException ex) {
            Logger.getLogger(Izmena.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        ukupno = new javax.swing.JTextField();
        su = new javax.swing.JTextField();
        brojsati = new javax.swing.JTextField();
        radnisat = new javax.swing.JTextField();
        prevoz = new javax.swing.JTextField();
        pausalno = new javax.swing.JTextField();
        sudija = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        mesto = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        napomena = new javax.swing.JTextArea();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Евиденција поротника - Измена");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel5.setText("Изабрано решење:");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 20, -1, -1));

        jLabel2.setText("Поротник:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 60, -1, -1));

        jLabel3.setText("Судија:");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 60, -1, -1));

        jLabel6.setText("СУ број:");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 110, -1, -1));

        jLabel8.setText("Датум:");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 170, -1, -1));

        jLabel10.setText("Број сати:");
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 220, -1, -1));

        jLabel12.setText("Цена по сату:");
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 280, -1, -1));

        jLabel14.setText("Превоз:");
        getContentPane().add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 330, -1, -1));

        jLabel16.setText("Паушално:");
        getContentPane().add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 380, -1, -1));

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel18.setText("Укупно:");
        getContentPane().add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 290, -1, -1));
        getContentPane().add(ukupno, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 280, 160, 40));

        su.setEditable(false);
        getContentPane().add(su, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 110, 250, 30));

        brojsati.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                brojsatiKeyReleased(evt);
            }
        });
        getContentPane().add(brojsati, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 220, 250, 30));
        getContentPane().add(radnisat, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 280, 250, 30));
        getContentPane().add(prevoz, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 330, 250, 30));
        getContentPane().add(pausalno, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 380, 250, 30));
        getContentPane().add(sudija, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 60, 250, 30));

        jButton1.setBackground(new java.awt.Color(255, 51, 51));
        jButton1.setForeground(new java.awt.Color(255, 255, 255));
        jButton1.setText("Одустани");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(660, 370, 90, 50));

        jButton2.setBackground(new java.awt.Color(153, 255, 153));
        jButton2.setText("Сними");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 370, 90, 50));
        getContentPane().add(jDateChooser1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 160, 130, 30));
        getContentPane().add(mesto, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 110, 250, 30));

        jLabel1.setText("Место:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 120, -1, -1));

        jLabel4.setText("Напомена:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 170, -1, -1));

        napomena.setColumns(20);
        napomena.setRows(5);
        jScrollPane1.setViewportView(napomena);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 160, 350, -1));

        getContentPane().add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 60, 250, 30));

        getContentPane().add(jComboBox2, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 20, 250, -1));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-912)/2, (screenSize.height-487)/2, 912, 487);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
         new TableFromDatabase().setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        
        java.sql.Connection connection = null;

        try {
            Date dateFromDateChooser = jDateChooser1.getDate();
            String dateString = String.format("%1$td-%1$tm-%1$tY", dateFromDateChooser);
            connection = DriverManager.getConnection(url, username, password);
            java.sql.PreparedStatement i = connection.prepareStatement("UPDATE evidencija set  datum=? , ime_porotnika = ? , mesto = ?, ime_sudije = ? , broj_sati = ? , po_satu = ?,prevoz = ?, pausalno=?,ukupno=?,napomena=? where su = " + su.getText() + "");
            i.setString(1, dateString);
            i.setString(2, jComboBox1.getSelectedItem().toString());
            i.setString(3, mesto.getText());
            i.setString(4, jComboBox2.getSelectedItem().toString());
            i.setString(5, brojsati.getText());
            i.setString(6, radnisat.getText());
            i.setString(7, prevoz.getText());
            i.setString(8, pausalno.getText());
            i.setString(9, ukupno.getText());
            i.setString(10, napomena.getText());
            i.executeUpdate();
            connection.close();

            this.dispose();
           new TableFromDatabase().setVisible(true);


        } catch (SQLException ex) {
            Logger.getLogger(Izmena.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void brojsatiKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_brojsatiKeyReleased
        rsq = Float.parseFloat(radnisat.getText());
        prev = Float.parseFloat(prevoz.getText());
        pau = Float.parseFloat(pausalno.getText());
        String brsati = brojsati.getText();
        BrojSati = Integer.parseInt(brsati);
        Float U = (BrojSati * rsq) + prev + pau;
        ukupno.setText(U.toString());
    }//GEN-LAST:event_brojsatiKeyReleased

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Izmena().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField brojsati;
    private javax.swing.JButton jButton1;
    javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField mesto;
    private javax.swing.JTextArea napomena;
    private javax.swing.JTextField pausalno;
    private javax.swing.JTextField prevoz;
    private javax.swing.JTextField radnisat;
    private javax.swing.JTextField su;
    private javax.swing.JTextField sudija;
    private javax.swing.JTextField ukupno;
    // End of variables declaration//GEN-END:variables
}
